<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ totle }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
          <el-button type="danger" size="small" @click="handleDownload">excel导出</el-button>
          <el-button type="primary" size="small" @click="hAdd">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{prop:'workNumber',order:'descending'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ hireTypeEnum(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" prop="" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail?id=${row.id}`)">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[1, 3, 5, 10]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totle"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog :visible.sync="showDialog" title="添加" @close="hClose">
        <empDialog
          ref="empdialog"
          @success="hSuccess"
          @close="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getEmployees, removeEmployees } from '@/api/employees'
import employeesEnum from '@/constant/employees.js'
import empDialog from './empDialog.vue'
import { excledexport } from '@/utils/index'

const hireType = employeesEnum.hireType.reduce((acc, e) => {
  acc[e.id] = e.value
  return acc
}, {})
export default {
  components: { empDialog },
  data() {
    return {
      list: [],
      page: 1,
      size: 2,
      totle: 0,
      showDialog: false
    }
  },
  created() {
    this.loadEmployees()
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page = 1
      this.size = val
      this.loadEmployees()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.loadEmployees()
    },
    hireTypeEnum(data) {
      return hireType[data] || '未知'
    },
    async  loadEmployees() {
      try {
        const { data: res } = await getEmployees(this.page, this.size)
        // console.log(res)
        if (res.code === 10000) {
          this.list = res.data.rows
          this.totle = res.data.total
        }
      } catch (error) {
        console.log(error)
      }
    },
    hAdd() {
      this.showDialog = true
    },
    hSuccess() {
      this.totle++
      const maxPage = Math.ceil(this.totle / this.size)
      this.page = maxPage
      this.loadEmployees()
      this.showDialog = false
    },

    // 退出清除表单
    hClose() {
      this.$refs.empdialog.resetForm()
    },

    // 删除
    hDel(id) {
      this.$confirm('确定删除吗？').then(
        async() => {
          const { data: res } = await removeEmployees(id)
          console.log(res)
          if (this.list.length === 1) {
            this.page--
            if (this.page < 0) {
              this.page = 1
            }
          }
          this.loadEmployees()
        }
      ).catch(err => err)
    },

    // 表格下载
    async handleDownload() {
      try {
        const { data: res } = await getEmployees()
        // console.log(res.data.rows)
        const { headers, data } = excledexport(res.data.rows)
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          excel.export_json_to_excel({
            header: headers,
            data,
            filename: this.filename,
            autoWidth: this.autoWidth,
            bookType: this.bookType
          })
          this.downloadLoading = false
        })
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
